import java.util.ArrayList;
import java.util.Vector;

public class EastWestSection extends Section {
	private Intersection eastern, western;
	private Vector<Vehicle> eastBound;
	private Vector<Vehicle> westBound;
	
	public EastWestSection (String name, Intersection eastern, Intersection western, double distance, int maxVehicles, Metric metric, String description) {
		super(name, distance, maxVehicles, metric, description);
		this.eastern = eastern;
		this.western = western;
		eastBound = new Vector<Vehicle>();
		westBound = new Vector<Vehicle>();
	}
	
	/**
	 * Accessor Method (Weight)
	 * @param direction The direction of the vehicle.
	 * @return The weight of the road section.
	 */
	public double getWeight(Direction direction) {
		double weight = 0;
		
		switch (direction) {
		case East:
			weight = metric.calculateWeight(distance, eastBound.size());
			break;
		case West:
			weight = metric.calculateWeight(distance, westBound.size());
			break;
		}
		
		return weight;
	}
	
	
	/**
	 * Accessor Method (Eastern Intersection)
	 * @return	The intersection to the east of the road section.
	 */
	public Intersection getEasternIntersection() {
		return eastern;
	}
	
	/**
	 * Accessor Method (Western Intersection)
	 * @return	The intersection to the west of the road section.
	 */
	public Intersection getWesternIntersection() {
		return western;
	}
	
	/**
	 * Adds a vehicle in either the east or west bound direction.
	 * Checks to see if traffic has not exceeded maxVehicles.
	 * @param vehicle 		The vehicle to be added.
	 * @param direction 	The direction in which to add the vehicle.
	 * @return True/false if vehicle could be added.
	 */
	public boolean addVehicle(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case East:
			if (eastBound.size() < maxVehicles) {
				eastBound.addElement(vehicle);
				success = true;
			}
			break;
		case West:
			if (westBound.size() < maxVehicles) {
				westBound.addElement(vehicle);
				success = true;
			}
			break;
		}

		return success;
	}

	public boolean addVehiclewithoutMax(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case East:
				eastBound.addElement(vehicle);
				success = true;
			
			break;
		case West:
				westBound.addElement(vehicle);
				success = true;
			
			break;
		}

		return success;
	}

	/**
	 * Removes a vehicle from either the east or west bound direction.
	 * @param vehicle 		The vehicle to be removed.
	 * @param direction 	The direction in which to remove the vehicle.
	 * @return True/false if vehicle could be removed.
	 */
	public boolean removeVehicle(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case East:
			success = eastBound.removeElement(vehicle);
			break;
		case West:
			success = eastBound.removeElement(vehicle);
			break;
		}

		return success;
	}
	
	/**
	 * Returns the next intersection for this road given the last intersection and current direction
	 * @param direction 			The direction of the vehicle
	 * @return The next intersection the vehicle will hit.
	 */
	public Intersection nextIntersection(Direction direction) {
		Intersection temp = null;
		
		switch (direction) {
			case East:
				temp = eastern;
				break;
			case West:
				temp = western;
				break;			
		}

		return temp;
	}
	
	/***
	 * Returns the list of Intersection for the current section
	 * @param 
	 * @return A list of 2 intersections for the section
	 */
	
	public ArrayList<Intersection> getIntersections() {
		ArrayList<Intersection> intersections = new ArrayList<Intersection>();
		intersections.add(eastern);
		intersections.add(western);
		return intersections;
	}
	
	public int getSize(Direction dir){
		switch (dir) {
		case East:
			return eastBound.size();
		case West:
			return westBound.size();
	}

		return 1000;
	}
}